package com.eportal.action.background;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.domain.Sysopt;
import com.eportal.domain.TKeys;
import com.eportal.domain.UserAlbum;
import com.eportal.service.TUsersService;
import com.eportal.service.UserAlbumService;
import com.framework.jqgrid.JqGridPager;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 好邻居管理
 * @author chencheng
 *
 */
public class UserAlbumAction extends BaseAction implements ModelDriven<UserAlbum> {

    /**
	 * @author chencheng
	 */
	private static final long serialVersionUID = 1L;

	private static Logger log = Logger.getLogger(UserAlbumAction.class.getName());

    private Map<String, Object> result = new HashMap<String, Object>();

    @Autowired
    UserAlbumService userAlbumService;
    @Autowired
    private TUsersService tUsersService;
    
    /**
     * 好邻居列表
     */
    public void userAlbumList() throws Exception {
    	JqGridPager jqGridPager = new JqGridPager();
	   	 List<Map<String, Object>> userAlbumList = new ArrayList<Map<String,Object>>();
	   	 String userId = getRequest().getParameter("userId");
	   	 if (!tUsersService.haveRole(userId)) {
	   		 jqGridQueryMap(TKeys.class, getRequest(), userAlbumList, getResponse(), jqGridPager);
	   		 return;
		 }
	   	 StringBuilder where = new StringBuilder(" 1=1 ");
	   	 where.append(" and t.userid='");
 		 where.append(userId);
 		 where.append("'");
     	 
     	int userRole =  (Integer) getSession().getAttribute("userRole");
		Sysopt  user= (Sysopt)getSession().getAttribute("user");
    	if (userRole == 3 || userRole == 4) {
    		where.append(" and u.agtcode='");
    		where.append(user.getAgtCode());
       	 	where.append("'");
    	}
    	if (userRole == 5 || userRole == 6) {
    		where.append(" and t.communityId='");
    		where.append(user.getCommunityId());
    		where.append("'");
    	}
    	jqGridPager.setWhere(where.toString());
     	 setPage(getRequest(), jqGridPager);
     	 userAlbumList = userAlbumService.list(jqGridPager);
     	 
     	 jqGridQueryMap(TKeys.class, getRequest(), userAlbumList, getResponse(), jqGridPager);
    }
    
    /**
     * 好邻居置顶或取消置顶操作
     */
    public void albumOrderTop(){
    	String sid = getRequest().getParameter("sid");
    	String status = getRequest().getParameter("status");
    	result = userAlbumService.albumOrderTopMethod(sid,status);
        responseBody(result);
    }
    
    /**
     * 帖子删除
     */
    public void albumDelete() {
    	String sid = getRequest().getParameter("sid");
    	result = userAlbumService.deleteMethod(sid);
        responseBody(result);
    }
    

    
    private UserAlbum userAlbum = new UserAlbum();

    @Override
    public UserAlbum getModel() {
        return userAlbum;
    }
}
